package com.sy.api;

/**
 * @className: CourseController
 * @author: Mr.Liu
 * @date: 2025/10/29 14:37
 * @Version: 1.0
 * @description:
 */

import com.sy.PageVO;
import com.sy.ResultVO;
import com.sy.dto.CoursesByUidDTO;
import com.sy.dto.CoursesPageByUserDTO;
import com.sy.dto.CoursesPageDTO;
import com.sy.pojo.Courses;
import com.sy.service.CoursesService;
import com.sy.vo.HotCourseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/api/courses")
@Tag(name = "课程管理模块")
@Slf4j
@Data
@ConfigurationProperties(prefix = "hot.course")
public class CourseController {
    private Integer limit;
    @Resource
    private CoursesService coursesService;
    @PostMapping("/add")
    @Operation(summary = "添加课程")
    public ResultVO<Object> add(@RequestBody Courses courses) {
        log.info("添加的入参:{}",courses);
       //给courses中的createTime赋值
        courses.setCreateTime(LocalDateTime.now());
        boolean flag = coursesService.save(courses);
        return flag?ResultVO.success():ResultVO.error();
    }
    /*
     * @Author Mr.Liu
     * @Description //TODO 课程的分页查询及条件查询
     * @Date 17:23 2025/11/5
     * @Param [coursesPageDTO]
     * @return com.sy.ResultVO<java.lang.Object>
     **/
    @GetMapping("/page")
    @Operation(summary = "课程列表")
    public ResultVO<Object> queryPage(CoursesPageDTO coursesPageDTO) {
        log.info("课程分页的的入参:{}",coursesPageDTO);
        PageVO<Courses> pageVO = coursesService.queryPage(coursesPageDTO);
        return ResultVO.success(pageVO);
    }
    @GetMapping("/userPage")
    @Operation(summary = "用户端课程列表")
    public ResultVO<Object> queryPageByUser(CoursesPageByUserDTO coursesPageDTO) {
        log.info("课程分页的的入参:{}",coursesPageDTO);
        PageVO<Courses> pageVO = coursesService.queryCoursesPage(coursesPageDTO);
        return ResultVO.success(pageVO);
    }

    @GetMapping("/{id}")
    @Operation(summary = "用户端课程详情")
    public ResultVO<Object> courseDetail(@PathVariable Integer id) {
        log.info("课程ID是:{}",id);
        Courses courses = coursesService.queryById(id);
        return ResultVO.success(courses);
    }
    @PostMapping("/myCourse")
    @Operation(summary = "我的课程")
    public ResultVO<Object> myCourses(@RequestBody CoursesByUidDTO coursesByUidDTO) {
        log.info("我的课程的入参:{}",coursesByUidDTO);
        List<Courses> list = coursesService.queryCoursesByUid(coursesByUidDTO);
        if(list!=null&&list.size()>0){
            return ResultVO.success(list);
        }
        return ResultVO.error("当前用户没有购买任何课程");
    }

    @GetMapping("/hotCourse")
    @Operation(summary = "首页热门课程")
    public ResultVO<Object> hotCourse() {
        List<HotCourseVO> voList = coursesService.hotCourse(limit);
        return ResultVO.success(voList);
    }


}
